﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noix.Web;
using System.IO;

namespace NoixWebServer
{
    public partial class MainForm : Form
    {
        private HttpListenerController _controller = null;
        
        public MainForm()
        {
            InitializeComponent();
            InitializeControls();
            textBoxPort.Text = "80";
            textBoxVirtualDir.Text = "/";
            Environment.CurrentDirectory = Directory.GetCurrentDirectory() + @"\..\..\..\";
            textBoxPhysicalDir.Text = Directory.GetCurrentDirectory() + @"\WebRoot\";
        }

        public void InitializeControls()
        {
            linkLabelRootURL.Text = "";
            textBoxPort.Enabled = true;
            textBoxVirtualDir.Enabled = true;
            textBoxPhysicalDir.Enabled = true;
            buttonStart.Enabled = true;
            buttonStop.Enabled = false;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            string[] prefixes = new string[1];
            prefixes[0] = String.Format("http://*:{0}/",textBoxPort.Text);
            linkLabelRootURL.Text = String.Format("http://localhost:{0}{1}Default.aspx", textBoxPort.Text, textBoxVirtualDir.Text);
            textBoxPort.Enabled = false;
            textBoxVirtualDir.Enabled = false;
            textBoxPhysicalDir.Enabled = false;
            buttonStart.Enabled = false;
            buttonStop.Enabled = true;
            _controller = new HttpListenerController(prefixes, textBoxVirtualDir.Text, textBoxPhysicalDir.Text);
            _controller.Start();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            _controller.Stop();
            InitializeControls();
        }

        private void linkLabelRootURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(linkLabelRootURL.Text);
        }
    }
}
